package com.demo.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.demo.entity.Course;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: bj910
 * Date:2023-08-08
 * Time: 9:26
 * To change this template use File | Settings | File Templates.
 */
@Service
@DS("read")
//用ds注解切换数据源
//可以写在类上，可以写在方法上。快速的完成读写分离
public class CourseServiceV2 {
    @Resource
    private JdbcTemplate jdbcTemplate;

    public List selectCourse(){
        return jdbcTemplate.queryForList("select * from course");
    }


    @DS("write")
    public int createCourse1(Course course){
        int update = jdbcTemplate.update("insert into course value (?,?,?,?)",
                course.getCid(), course.getCname(), course.getUserId(), course.getCstatus());
        int i = 1/0;
        return update;
    }

    @DS("write")
    @DSTransactional//有事务控制 出现异常会回滚
    public int createCourse2(Course course){
        int update = jdbcTemplate.update("insert into course value (?,?,?,?)",
                course.getCid(), course.getCname(), course.getUserId(), course.getCstatus());
        int i = 1/0;
        return update;
    }
}
